package com.vitas.coin.vm

import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.snap.base.ktx.exitAccountDialog
import com.snap.base.ktx.isChecking
import com.snap.base.ktx.isLogin
import com.snap.base.ktx.isNeedBuy
import com.snap.base.ktx.isOnline
import com.snap.base.ktx.isTourist
import com.snap.base.ktx.startAbout
import com.snap.base.ktx.startAgreement
import com.snap.base.ktx.startFeedback
import com.snap.base.ktx.startPrivacy
import com.snap.base.util.BaseUtil
import com.snap.common.main.BaseVM
import com.snap.login.ktx.startAccountAct
import com.snap.login.ktx.startLoginAct
import com.snap.util.startAct
import com.snap.util.toast
import com.snap.wx.ktx.openWXCustomer
import com.vitas.coin.constant.AppConstant
import com.vitas.coin.ui.act.BuyAct
import com.vitas.coin.ui.act.MakeAct
import com.vitas.coin.ui.act.UploadAct
import kotlinx.coroutines.launch

class MineVM : BaseVM() {

    val isAudio = MutableLiveData<Boolean>().apply {
        value = BaseUtil.isChecking()
    }

    fun onClickFeed() = BaseUtil.startFeedback()

    fun onClickUser() = BaseUtil.startAgreement()

    fun onClickStartPrivacy() = BaseUtil.startPrivacy()

    fun onClickEmail() {
        BaseUtil.copyEmail(email = "hfldygkj@163.com")
    }


    lateinit var actionAct: () -> FragmentActivity

    lateinit var actionUpdate: suspend () -> Unit
    fun onClickUpgrade() {
        viewModelScope.launch {
            actionUpdate.invoke()
        }
    }



    fun onClickCreate(){
        if (BaseUtil.isTourist()) {
            BaseUtil.startLoginAct(false, firstColor = "#FFF4E6", goneQQ = true)
            return
        }
        startAct(MakeAct::class.java){
            putExtra(UploadAct.PARENT_ID,0L)
        }
//        startAct(UploadAct::class.java){
//            putExtra(UploadAct.PARENT_ID,0L)
//        }
    }

    fun clickLogin() {
        if (BaseUtil.isTourist()) {
            BaseUtil.startLoginAct(false, firstColor = "#FFF4E6", goneQQ = true)
            return
        }
        if (BaseUtil.isNeedBuy() && BaseUtil.isOnline()) {
            startAct(BuyAct::class.java)
        }
    }


    fun clickCustomerChat() {
        BaseUtil.openWXCustomer(success = {
        }, failed = {
            toast(it.message.toString())
        })
    }
    fun clickFeed() = BaseUtil.startFeedback()
    fun clickContact() = BaseUtil.copyEmail("hfldygkj@163.com")
    fun clickAbout() = BaseUtil.startAbout(customEmail = "hfldygkj@163.com")


    fun clickAccountSetting() {
        BaseUtil.startAccountAct()
    }

//    fun clickExitAccount() {
//        if (BaseUtil.isTourist()) {
//            toast("暂未登录")
//            return
//        }
//        BaseUtil.exitAccountDialog(actionAct.invoke(), true)
//    }
//
//    fun clickClearAccount() {
//        if (BaseUtil.isTourist()) {
//            toast("暂未登录")
//            return
//        }
//        BaseUtil.exitAccountDialog(actionAct.invoke(), false)
//    }

}